package com.xuecheng.content.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.TeachplanDTO;
import com.xuecheng.api.content.model.TeachplanMediaDTO;
import com.xuecheng.api.media.dto.MediaDTO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.common.enums.common.AuditEnum;
import com.xuecheng.common.enums.common.CommonEnum;
import com.xuecheng.common.enums.content.CourseAuditEnum;
import com.xuecheng.common.enums.content.CourseModeEnum;
import com.xuecheng.common.enums.content.TeachPlanEnum;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.content.common.constant.ContentErrorCode;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.convert.TeachplanConvert;
import com.xuecheng.content.convert.TeachplanMediaConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.entity.Teachplan;
import com.xuecheng.content.entity.TeachplanMedia;
import com.xuecheng.content.entity.exd.TeachplanNode;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanMediaService;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.openfeign.Media4Content;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程计划 服务实现类
 * </p>
 *
 * @author itcast
 */

@Slf4j
@Service
public class TeachplanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan> implements TeachplanService {

    @Autowired
    private CourseBaseService courseBaseService;

    @Autowired
    private TeachplanMediaService teachplanService;
    @Autowired
    private Media4Content media4Content;
    @Autowired
    private TeachplanMediaService teachplanMediaService;

    /**
     * @param courseBaseId
     * @param companyId
     * @return java.util.Listcom.xuecheng.api.content.model.TeachplanDTO  返回数据
     * @Description //TODO
     * @Date 2021/12/11 19:12
     * @Param [courseBaseId, Long  课程id
     * companyId]   Long  教育机构id
     * <p>
     * 业务分析
     * 1:不用开启事务
     * 2:关键数据
     * 课程id不能为空
     * 教育机构id不能为空
     * 3:业务数据
     * 教育机构id是否正确
     * 课程是否已经被删除
     * 4:查询课程表--判断业务数据
     * 5:查询课程计划表
     * 6:运用递归将其生成树状结构
     * 7:将查询到的结果集转化为DTO实体类
     * 8:返回数据
     */
    @Override
    public TeachplanDTO queryTeachplanList(Long courseBaseId, Long companyId) {
        //判断关键数据
        if (ObjectUtils.isEmpty(courseBaseId) || ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(ContentErrorCode.E_120028);
        }
        //根据id查询课程表并判断业务数据
        LambdaQueryWrapper<CourseBase> queryWrapperCouse = new LambdaQueryWrapper<>();
        queryWrapperCouse.eq(CourseBase::getId, courseBaseId);
        CourseBase courseBase = courseBaseService.getOne(queryWrapperCouse);
        if (ObjectUtils.isEmpty(courseBase)) {
            ExceptionCast.cast(ContentErrorCode.E_120033);
        }
        if ((courseBase.getStatus().equals(CommonEnum.DELETE_FLAG.getCodeInt()))) {
            ExceptionCast.cast(ContentErrorCode.E_120029);
        }
        if (!(companyId.equals(courseBase.getCompanyId()))) {
            ExceptionCast.cast(ContentErrorCode.E_120030);
        }

//        5:查询课程计划表
        TeachplanMapper baseMapper = this.getBaseMapper();
        List<TeachplanNode> queryTeachplanList = baseMapper.queryTeachplanList(courseBaseId);
        //如果其查询为空.返回null
        TeachplanDTO dto = null;
        if (ObjectUtils.isEmpty(queryTeachplanList)) {
            dto = new TeachplanDTO();
        } else {
            //删除其第一层数据并返回数据
            TeachplanNode queryOne = queryTeachplanList.remove(0);
            //调用递归方式获取其树形结构
            queryTeachplan(queryOne, queryTeachplanList);
            //调用工具类将其转化为DTO类并返回
            dto = CourseBaseConvert.INSTANCE.note2DTO(queryOne);
        }
        return dto;
    }

    /**
     * @param teachplanDTO
     * @param companyId
     * @return com.xuecheng.api.content.model.TeachplanDTO
     * @Description //修改或者添加--根据teachplanid和parentid来判断几级数据的增加或者修改
     * @Date 2021/12/11 20:52
     * @Param [teachplanDTO, companyId]
     * 业务分析
     * 是否需要开启事务-->增删改需要事务
     * 关键数据判断-->有无teachplanid决定添加或者修改
     * 课程是否已经被逻辑删除
     * 教育机构有没有
     * 课程的计划资源类型
     * 课程计划名称
     * 课程id
     * <p>
     * 业务数据--->教育机构的核对
     * 是否存在课程
     * 是否已经被逻辑删除
     * 决定添加或者修改
     * 添加几级数据-->
     * 修改几级数据-->有无parentid来判断
     * 返回数据
     */
    @Override
    @Transactional
    public TeachplanDTO updateorInsertTeachplan(TeachplanDTO teachplanDTO, Long companyId) {
        //判断课程id和教育机构id是否存在
        if (ObjectUtils.isEmpty(companyId) || ObjectUtils.isEmpty(teachplanDTO.getCourseId())) {
            ExceptionCast.cast(ContentErrorCode.E_120028);
        }
        //判断有无teachplanid决定添加修改
        if (ObjectUtils.isEmpty(teachplanDTO.getTeachPlanId())) {
            //添加数据
            Teachplan insertTeachplan = insertTeachplan(teachplanDTO, companyId);
            TeachplanDTO teachplanDTO1 = TeachplanConvert.INSTANCE.po2DTO(insertTeachplan);
            return teachplanDTO1;

        } else {
            //修改
            return updateTeachplan(teachplanDTO, companyId);

        }
    }

    /**
     * @param teachPlanId
     * @param companyId
     * @return void
     * @Description //根据课程计划的id删除
     * @Date 2021/12/11 23:56
     * @Param [teachPlanId, companyId]  课程计划的id+教育机构id
     * 业务分析
     * 增刪改需要開啓食物
     * 关键数据
     * 教育机构+课程计划id必须有
     * 业务数据
     * 子数据下如果有数据不能删除
     * 删除章节时如果有关联资源.不能删除
     * 删除返回.不用返回值
     */
    @Override
    @Transactional
    public void removeTeachPlan(Long teachPlanId, Long companyId) {
        //关键数据判断
        if (ObjectUtils.isEmpty(teachPlanId) || ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(ContentErrorCode.E_120030);
        }

        //判断其是几级课程
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getId, teachPlanId);
        Teachplan teachplan = this.getOne(queryWrapper);
        //取出其等级值
        Integer grade = teachplan.getGrade();
        //取出其父类id
        Long parentid = teachplan.getParentid();
        //取出其课程基本id值
        Long courseId = teachplan.getCourseId();
        //根据课程id与机构id查询-->是否尤其对应的课程
        LambdaQueryWrapper<CourseBase> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseBase::getId, courseId);
        wrapper.eq(CourseBase::getCompanyId, companyId);
        CourseBase courseBase = courseBaseService.getOne(wrapper);
        if (ObjectUtils.isEmpty(courseBase)) {
            ExceptionCast.cast(ContentErrorCode.E_120028);
        }
        //判断是否已经被删除.是否为未提交与审核未通过状态
        if (courseBase.getStatus().equals(CommonEnum.DELETE_FLAG.getCodeInt())) {
            ExceptionCast.cast(ContentErrorCode.E_120031);
        }
        if (!courseBase.getAuditStatus().equals(CourseAuditEnum.AUDIT_DISPAST_STATUS.getCode()) &&
                !courseBase.getAuditStatus().equals(CourseAuditEnum.AUDIT_UNPAST_STATUS.getCode())) {
            ExceptionCast.cast(ContentErrorCode.E_120011);
        }
        //判断课程等级--->如果当前状态为第二大数据
        if (grade.equals(TeachPlanEnum.SECEND_LEVEL)) {
            //判断其是否有子数据
            LambdaQueryWrapper<Teachplan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Teachplan::getParentid, teachPlanId);
            List<Teachplan> list = this.list(lambdaQueryWrapper);
            if (!(CollectionUtils.isEmpty(list))) {
                ExceptionCast.cast(ContentErrorCode.E_120036);
            }
        }
        //如果此时为第三课程
        if (grade.equals(TeachPlanEnum.THIRD_LEVEL)) {
            //判断第三课程是否关联了媒体资源
            LambdaQueryWrapper<TeachplanMedia> wrapper1 = new LambdaQueryWrapper<TeachplanMedia>();
            wrapper1.eq(TeachplanMedia::getTeachplanId, teachPlanId);
            List<TeachplanMedia> listMedia = teachplanService.list(wrapper1);
            if (!(ObjectUtils.isEmpty(listMedia))) {
                ExceptionCast.cast(ContentErrorCode.E_120036);
            }
        }
        if (grade.equals(TeachPlanEnum.FIRST_LEVEL)) {
            ExceptionCast.cast(ContentErrorCode.E_120036);
        }

        boolean b = this.removeById(teachPlanId);
        log.info("当前是否被删除----->{}", b);


    }

    /**
     * @param dto
     * @param companyId
     * @return com.xuecheng.api.content.model.TeachplanMediaDTO
     * @Description //TODO 课程计划与媒资信息的绑定
     * @Date 2021/12/16 19:20
     * @Param [dto, companyId]
     * 业务分析
     * 开启事务
     * 关键数据-->机构id
     */
    @Override
    @Transactional
    public TeachplanMediaDTO associateMedia(TeachplanMediaDTO dto, Long companyId) {

        // 1.判断关键数据
        //      teachplanid mediaid companyId
        if (ObjectUtils.isEmpty(dto.getTeachplanId()) ||
                ObjectUtils.isEmpty(dto.getMediaId()) ||
                ObjectUtils.isEmpty(companyId)
        ) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }


        LambdaQueryWrapper<Teachplan> teachplanQueryWrapper = new LambdaQueryWrapper<>();

        teachplanQueryWrapper.eq(Teachplan::getId, dto.getTeachplanId());
        teachplanQueryWrapper.eq(Teachplan::getGrade, TeachPlanEnum.THIRD_LEVEL);

        Teachplan teachplan = this.getOne(teachplanQueryWrapper);

        if (ObjectUtils.isEmpty(teachplan)) {
            ExceptionCast.cast(ContentErrorCode.E_120402);
        }

        //       课程基础信息
        //          判断是否是同一家机构的操作
        //              根据课程基础信息来查询
        //              判断课程类型是否是点播
        LambdaQueryWrapper<CourseBase> baseQueryWrapper = new LambdaQueryWrapper<>();

        baseQueryWrapper.eq(CourseBase::getId, teachplan.getCourseId());
        baseQueryWrapper.eq(CourseBase::getCompanyId, companyId);
        baseQueryWrapper.eq(CourseBase::getTeachmode, CourseModeEnum.COURSE_MODE_RECORD_STATUS.getCode());
        baseQueryWrapper.eq(CourseBase::getStatus, CommonEnum.USING_FLAG.getCodeInt());

        CourseBase courseBase = courseBaseService.getOne(baseQueryWrapper);
        // 课程计划和媒资信息绑定要求课程审核状态为：未提交和审核未通过
        String auditStatus = courseBase.getAuditStatus();
        if (CourseAuditEnum.AUDIT_PASTED_STATUS.getCode().equals(auditStatus) ||
                CourseAuditEnum.AUDIT_COMMIT_STATUS.getCode().equals(auditStatus) ||
                CourseAuditEnum.AUDIT_PUBLISHED_STATUS.getCode().equals(auditStatus)
        ) {
            ExceptionCast.cast(ContentErrorCode.E_120015);
        }
        //      媒资数据
        //          判断是否存在
        //          判断是否是同一家机构
        //          判断媒资信息是否审核通过
        RestResponse<MediaDTO> restResponse = media4Content.getMediaById(dto.getMediaId());
        if (!(restResponse.isSuccessful())) {
            ExceptionCast.castWithCodeAndDesc(restResponse.getCode(), restResponse.getMsg());
        }

//        MediaDTO mediaDTO = restResponse.getResult();
        MediaDTO mediaDTO = restResponse.getResult();


        if (!(mediaDTO.getCompanyId().equals(companyId))) {
            ExceptionCast.cast(CommonErrorCode.E_100108);
        }

        String mediaAuditStatus = mediaDTO.getAuditStatus();

        if (!(AuditEnum.AUDIT_PASTED_STATUS.getCode().equals(mediaAuditStatus))) {
            ExceptionCast.cast(ContentErrorCode.E_120038);
        }
        //      判断关联数据是否存在
        LambdaQueryWrapper<TeachplanMedia> teachplanMediaQueryWrapper = new LambdaQueryWrapper<>();
        teachplanMediaQueryWrapper.eq(TeachplanMedia::getTeachplanId, teachplan.getId());
        teachplanMediaQueryWrapper.eq(TeachplanMedia::getCourseId, teachplan.getCourseId());

        int count = teachplanMediaService.count(teachplanMediaQueryWrapper);

        boolean result = false;
        if (count < 1) {
            //          如果不存在
            //              创建数据
            TeachplanMedia teachplanMedia = new TeachplanMedia();
            teachplanMedia.setMediaId(mediaDTO.getId());
            teachplanMedia.setTeachplanId(teachplan.getId());
            teachplanMedia.setCourseId(teachplan.getCourseId());
            teachplanMedia.setMediaFilename(mediaDTO.getFilename());

            result = teachplanMediaService.save(teachplanMedia);


        } else {
            //          如果存在
            //              修改数据
            //                  mediaid 和 medianame
            LambdaUpdateWrapper<TeachplanMedia> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(TeachplanMedia::getMediaId, mediaDTO.getId());
            updateWrapper.set(TeachplanMedia::getMediaFilename, mediaDTO.getFilename());
            updateWrapper.eq(TeachplanMedia::getTeachplanId, teachplan.getId());

            result = teachplanMediaService.update(updateWrapper);
        }

        if (!result) {
            ExceptionCast.cast(ContentErrorCode.E_120411);
        }


        //  4.将数据封装成dto并返回
        LambdaQueryWrapper<TeachplanMedia> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeachplanMedia::getTeachplanId, teachplan.getId());

        TeachplanMedia teachplanMedia = teachplanMediaService.getOne(queryWrapper);

        TeachplanMediaDTO resultDTO = TeachplanMediaConvert.INSTANCE.entity2dto(teachplanMedia);


        return resultDTO;
    }

    //实现修改
    //业务
    private TeachplanDTO updateTeachplan(TeachplanDTO dto, Long companyId) {


        // 判断业务数据
        LambdaQueryWrapper<CourseBase> queryWrapperCouse = new LambdaQueryWrapper<>();
        queryWrapperCouse.eq(CourseBase::getId, dto.getCourseId());
        CourseBase courseBase = courseBaseService.getOne(queryWrapperCouse);
        if (ObjectUtils.isEmpty(courseBase)) {
            ExceptionCast.cast(ContentErrorCode.E_120033);
        }
        if ((courseBase.getStatus().equals(CommonEnum.DELETE_FLAG.getCodeInt()))) {
            ExceptionCast.cast(ContentErrorCode.E_120029);
        }
        if (!(companyId.equals(courseBase.getCompanyId()))) {
            ExceptionCast.cast(ContentErrorCode.E_120030);
        }


        //     课程计划
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getId, dto.getTeachPlanId());

        int count = this.count(queryWrapper);

        if (count < 1) {
            ExceptionCast.cast(ContentErrorCode.E_120402);
        }
        //     pname
        //     mediatype
        //     startime
        //     endtime
        //     decription
        //     id_preview
        LambdaUpdateWrapper<Teachplan> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Teachplan::getPname, dto.getPname());
        updateWrapper.set(Teachplan::getMediaType, dto.getMediaType());
        updateWrapper.set(Teachplan::getStartTime, dto.getStartTime());
        updateWrapper.set(Teachplan::getEndTime, dto.getEndTime());
        updateWrapper.set(Teachplan::getDescription, dto.getDescription());
        updateWrapper.set(Teachplan::getIsPreview, dto.getIsPreview());
        updateWrapper.set(Teachplan::getChangeDate, LocalDateTime.now());
        updateWrapper.eq(Teachplan::getId, dto.getTeachPlanId());

        boolean result = this.update(updateWrapper);

        if (!result) {
            ExceptionCast.cast(ContentErrorCode.E_120407);
        }
        // 将数据库最新数据转为dto并返回
        Teachplan teachplan = this.getById(dto.getTeachPlanId());
        TeachplanDTO resultDTO = TeachplanConvert.INSTANCE.po2DTO(teachplan);
        return resultDTO;
    }

    /**
     * 业务分析
     * 关键数据
     * 业务数据
     **/


    private Teachplan insertTeachplan(TeachplanDTO teachplanDTO, Long companyId) {
        //查询课程基本数据操作
        Long courseId = teachplanDTO.getCourseId();
        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseBase::getId, courseId);
        queryWrapper.eq(CourseBase::getCompanyId, companyId);
        CourseBase courseBase = courseBaseService.getOne(queryWrapper);
        //判断其中给的业务数据是否支持此次添加操作
        if (ObjectUtils.isEmpty(courseBase)) {
            ExceptionCast.cast(ContentErrorCode.E_120026);
        }
        if (CommonEnum.DELETE_FLAG.getCodeInt().equals(courseBase.getStatus())) {
            ExceptionCast.cast(ContentErrorCode.E_120029);
        }
        //判断是添加二级数据还是三级数据
        //二级么有parentid
        Long parentid = teachplanDTO.getParentid();
        if (ObjectUtils.isEmpty(parentid)) {
            //添加二级数据
            //1:判断是否存在一级数据,没有先添加一级数据’
            LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Teachplan::getCourseId, teachplanDTO.getCourseId());
            wrapper.eq(Teachplan::getParentid, TeachPlanEnum.FIRST_PARENTID_FLAG);
//            TeachplanDTO one = this.getOne(wrapper);
            Teachplan oneQuyse = this.getOne(wrapper);
            //获取一级数据id
            if (ObjectUtils.isEmpty(oneQuyse)) {
                //实行添加一级数据
                //         如果没有：
                //             添加一级课程计划
                Teachplan firstNode = new Teachplan();
                firstNode.setPname(courseBase.getName());
                firstNode.setParentid(new Long(TeachPlanEnum.FIRST_PARENTID_FLAG.toString()));
                firstNode.setGrade(TeachPlanEnum.FIRST_LEVEL);
                firstNode.setOrderby(TeachPlanEnum.FIRST_LEVEL);
                firstNode.setCourseId(courseBase.getId());
                boolean result = this.save(firstNode);
                //判断添加是否成功
                if (!result) {
                    ExceptionCast.cast(ContentErrorCode.E_120033);
                }

            }
            //根据课程id和等级取ordeyby
            LambdaQueryWrapper<Teachplan> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(Teachplan::getCourseId, teachplanDTO.getCourseId());
            wrapper2.eq(Teachplan::getGrade, TeachPlanEnum.SECEND_LEVEL);
            int count = this.count(wrapper2);
            //开始添加二级数据
            //获取一级数据id
            LambdaQueryWrapper<Teachplan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Teachplan::getCourseId, courseBase.getId());
            lambdaQueryWrapper.eq(Teachplan::getParentid, TeachPlanEnum.FIRST_PARENTID_FLAG);
            Teachplan teachplan = this.getOne(lambdaQueryWrapper);

            teachplanDTO.setParentid(teachplan.getId());
            teachplanDTO.setGrade(TeachPlanEnum.SECEND_LEVEL);
            teachplanDTO.setOrderby(count + 1);
            Teachplan teachplan1 = TeachplanConvert.INSTANCE.dto2Po(teachplanDTO);
            boolean save = this.save(teachplan1);
            if (!save) {
                ExceptionCast.cast(ContentErrorCode.E_120031);
            }
            //查询添加后的数据并返回
            LambdaQueryWrapper<Teachplan> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(Teachplan::getId, teachplan1.getId());
            Teachplan one = this.getOne(wrapper1);
            return one;
        } else {
            //添加三级数据
            //查询是否有二级数据
            LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(Teachplan::getCourseId, courseId);
            wrapper.eq(Teachplan::getParentid, teachplanDTO.getParentid());
            wrapper.eq(Teachplan::getCourseId, teachplanDTO.getCourseId());
            List<Teachplan> list = this.list(wrapper);

            /////////////////////////////有问题、、、、//////////////////
//            if (CollectionUtils.isEmpty(list)) {
//                ExceptionCast.cast(ContentErrorCode.E_120033);
//            }
            //设置三级数据属性
            //根据课程id和等级取ordeyby
            LambdaQueryWrapper<Teachplan> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(Teachplan::getCourseId, teachplanDTO.getCourseId());
            wrapper2.eq(Teachplan::getGrade, TeachPlanEnum.THIRD_LEVEL);
            int count = this.count(wrapper2);
            //
            teachplanDTO.setGrade(TeachPlanEnum.THIRD_LEVEL);
            teachplanDTO.setOrderby(count + 1);
            Teachplan teachplan = TeachplanConvert.INSTANCE.dto2Po(teachplanDTO);
            boolean save = this.save(teachplan);
            if (!save) {
                ExceptionCast.cast(ContentErrorCode.E_120035);
            }
            LambdaQueryWrapper<Teachplan> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(Teachplan::getId, teachplan.getId());
            Teachplan one1 = this.getOne(wrapper1);
            return one1;
        }
    }

//    //二级和三级数据的添加操作
//    private TeachplanDTO apple(TeachplanDTO teachplanDTO) {
//        //获取上一级的
//        return null;
//    }


    private void queryTeachplan(TeachplanNode queryOne, List<TeachplanNode> queryTeachplanList) {
        //判断List<TeachplanNode> teachplanNode;集合是否为空.为空则创建
        if (CollectionUtils.isEmpty(queryOne.getTeachPlanTreeNodes())) {
            queryOne.setTeachPlanTreeNodes(new ArrayList<>());
        }
        //遍历课程计划集合
        for (TeachplanNode teachplanNode : queryTeachplanList) {
            //判断其pareanid是否与queryOne--id一致
            if (queryOne.getId().equals(teachplanNode.getParentid())) {
                //如果相同即是其关联的下级数据--将其添加进上级数据中
                queryOne.getTeachPlanTreeNodes().add(teachplanNode);
                //判断其等级数据时否为3.如果不为3,则无线调用其本身方法实现递归
                if (!(teachplanNode.getGrade().equals(TeachPlanEnum.THIRD_LEVEL))) {
                    queryTeachplan(teachplanNode, queryTeachplanList);
                }else {
                    //如果时第三级则查询和其绑定的相关的媒资信息
                    LambdaQueryWrapper<TeachplanMedia> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper.eq(TeachplanMedia ::getTeachplanId,teachplanNode.getId());
                    TeachplanMedia serviceOne = teachplanMediaService.getOne(lambdaQueryWrapper);
                    if (!(ObjectUtils.isEmpty(serviceOne))){
                        teachplanNode.setTeachplanMedia(serviceOne);
                    }
                }
            }
        }
    }
}